package cn.edu.cdp.gui.panel;

import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.table.*;

import cn.edu.cdp.dao.StudentDao;
import cn.edu.cdp.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;

@Component
public class StudentPanel extends JPanel {
    private final StudentDao studentDao;

    @Autowired
    public StudentPanel(StudentDao studentDao) {
        initComponents();
        this.studentDao = studentDao;
    }

    public void decoration(Student student) {
        textFieldId.setText(student.getStudentId());
        textFieldName.setText(student.getName());
        textFieldSex.setText(student.getSex());
        textFieldBirthday.setText(student.getBirthday().toString());
        textFieldCEE.setText(student.getCeeScore() + "");
        textFieldNation.setText(student.getNation());
        textFieldPlace.setText(student.getNativePlace());
        textFieldMajor.setText(student.getMajorName());
        textFieldResume.setText(student.getResume());

        // 设置头像
        String url = student.getProfile();
        ImageIcon imageIcon = new ImageIcon(url);
        Image scaledInstance = imageIcon.getImage().getScaledInstance(70, 100, Image.SCALE_SMOOTH);
        labelProfile.setIcon(new ImageIcon(scaledInstance));


        //======== scrollPane1 ========
        {
            List<Map<String, Object>> scoreList = studentDao.scoreList(student.getStudentId());
            Vector<Vector<String>> tableData = new Vector<>();
            for (Map<String, Object> map : scoreList) {
                Vector<String> row = new Vector<>();
                row.add((String) map.get("semester"));
                row.add((String) map.get("name"));
                row.add(map.get("score").toString());
                row.add(map.get("credit").toString());
                tableData.add(row);
            }


            Vector<String> tableTitle = new Vector<>();
            tableTitle.add("学期");
            tableTitle.add("课程名称");
            tableTitle.add("成绩");
            tableTitle.add("学分");
            //---- table1 ----
            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;   // 禁用在表格中编辑
                }
            });
//            scrollPane1.setViewportView(table1);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("swing_text");
        label1 = new JLabel();
        textFieldId = new JTextField();
        label2 = new JLabel();
        textFieldName = new JTextField();
        label3 = new JLabel();
        textFieldSex = new JTextField();
        label4 = new JLabel();
        textFieldBirthday = new JTextField();
        labelProfile = new JLabel();
        label5 = new JLabel();
        textFieldCEE = new JTextField();
        label6 = new JLabel();
        textFieldNation = new JTextField();
        label7 = new JLabel();
        textFieldPlace = new JTextField();
        label8 = new JLabel();
        textFieldMajor = new JTextField();
        label9 = new JLabel();
        textFieldResume = new JTextField();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {25, 65, 125, 65, 125, 65, 125, 65, 125, 25, 20, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {35, 35, 35, 35, 300, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText(bundle.getString("StudentPanel.label1.text"));
        add(label1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldId ----
        textFieldId.setEditable(false);
        textFieldId.setFocusable(false);
        add(textFieldId, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label2 ----
        label2.setText(bundle.getString("StudentPanel.label2.text"));
        add(label2, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldName ----
        textFieldName.setEditable(false);
        textFieldName.setFocusable(false);
        add(textFieldName, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label3 ----
        label3.setText(bundle.getString("StudentPanel.label3.text"));
        add(label3, new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldSex ----
        textFieldSex.setEditable(false);
        textFieldSex.setFocusable(false);
        add(textFieldSex, new GridBagConstraints(6, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label4 ----
        label4.setText(bundle.getString("StudentPanel.label4.text"));
        add(label4, new GridBagConstraints(7, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldBirthday ----
        textFieldBirthday.setEditable(false);
        textFieldBirthday.setFocusable(false);
        add(textFieldBirthday, new GridBagConstraints(8, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- labelProfile ----
        labelProfile.setText("text");
        labelProfile.setPreferredSize(new Dimension(70, 100));
        labelProfile.setMinimumSize(new Dimension(70, 100));
        add(labelProfile, new GridBagConstraints(9, 0, 1, 4, 0.0, 0.0,
            GridBagConstraints.NORTH, GridBagConstraints.NONE,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label5 ----
        label5.setText(bundle.getString("StudentPanel.label5.text"));
        add(label5, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldCEE ----
        textFieldCEE.setEditable(false);
        textFieldCEE.setFocusable(false);
        add(textFieldCEE, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label6 ----
        label6.setText(bundle.getString("StudentPanel.label6.text"));
        add(label6, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldNation ----
        textFieldNation.setEditable(false);
        textFieldNation.setFocusable(false);
        add(textFieldNation, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label7 ----
        label7.setText(bundle.getString("StudentPanel.label7.text"));
        add(label7, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldPlace ----
        textFieldPlace.setEditable(false);
        textFieldPlace.setFocusable(false);
        add(textFieldPlace, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label8 ----
        label8.setText(bundle.getString("StudentPanel.label8.text"));
        add(label8, new GridBagConstraints(7, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldMajor ----
        textFieldMajor.setEditable(false);
        textFieldMajor.setFocusable(false);
        add(textFieldMajor, new GridBagConstraints(8, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label9 ----
        label9.setText("\u7b80\u5386");
        add(label9, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldResume ----
        textFieldResume.setEditable(false);
        textFieldResume.setFocusable(false);
        add(textFieldResume, new GridBagConstraints(2, 2, 7, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, new GridBagConstraints(1, 4, 9, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 5), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textFieldId;
    private JLabel label2;
    private JTextField textFieldName;
    private JLabel label3;
    private JTextField textFieldSex;
    private JLabel label4;
    private JTextField textFieldBirthday;
    private JLabel labelProfile;
    private JLabel label5;
    private JTextField textFieldCEE;
    private JLabel label6;
    private JTextField textFieldNation;
    private JLabel label7;
    private JTextField textFieldPlace;
    private JLabel label8;
    private JTextField textFieldMajor;
    private JLabel label9;
    private JTextField textFieldResume;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
